package com.siyuanren.gcocp.common.web.handler;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.exception.BaseException;
import com.siyuanren.gcocp.consts.GcocpConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by   guwr
 * Project_name education
 * Path         com.siyuanren.gcocp.common.web.handler.WebHandlerExceptionResolver
 * Date         2017/3/14
 * Time         10:48
 * Description
 */
@Slf4j
@Component
public class WebHandlerExceptionResolver implements HandlerExceptionResolver {


    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();
        /* 使用FastJson提供的FastJsonJsonView视图返回，不需要捕获异常*/
        FastJsonJsonView view = new FastJsonJsonView();
        String message = e.getMessage();
        Map<String, Object> attributes = new LinkedHashMap<>();
        Integer code;
        if (e instanceof BaseException) {
            BaseException baseException = (BaseException) e;
            code = baseException.getCode();
            // 未登录
            log.info("code=" + code);
            if (Objects.equals(GcocpConsts._ONE, code)) {
                message = "状态已过期，请重新登录";

                String requestURI = request.getRequestURI();
                String redirectUrl = PublicConfig.PUBLIC_WX_URL.concat(requestURI);
                String queryString = request.getQueryString();
                if (StringUtils.isNotBlank(queryString)) {
                    redirectUrl = redirectUrl.concat("?").concat(queryString);
                }
                log.info("redirectUrl={}", redirectUrl);
                String redirect = PublicConfig.TOMCAT_URL
                        .concat(PublicConfig.PUBLIC_WX_AUTHORIZE_URL)
                        .concat("?redirectUrl=")
                        .concat(Base64.encode(redirectUrl));
                return new ModelAndView("redirect:".concat(redirect));
            } else {
                message = baseException.getMessage();
            }
        } else {
            log.info("message = {}", message);
            message = BaseException.SERVER_ERROR.getMsg();
            code = BaseException.SERVER_ERROR.getCode();
        }
        attributes.put("code", code);
        attributes.put("msg", message);
        attributes.put("data", new Object[]{});
        view.setAttributesMap(attributes);
        mv.setView(view);
        log.info("异常:" + message, e);
        return mv;
    }
}
